/*
 * Alexey Danchuk
 * This software is an implementation of the task from OOP module.
 */
package knowledgetestsystem;

import java.util.Scanner;

/**
 * This class describes intefaces to manipulate questions.
 * 
 * @version 2.03 10 February 2013
 * @author Alexey Danchuk
 */
abstract class Manipulator {

	public static final int SIMPLE_ANSWER_FLAG = 1;
	private static final int TEST_SIZE = 2;
	private static Question[] systemQuestions = new Question[TEST_SIZE];

	/**
	 * Method to detect question type.
	 * 
	 * @return question - one of three types of questions
	 */
	private static Question questionTypeDetect() {
		InOut input = new InOut();
		String inQuestion = null;
		String[] inAnswers = null;
		String[] correctAnswer = null;
		try {
			inQuestion = input.inputQuestion();
			inAnswers = input.inputAnswers();
			correctAnswer = input.inputCorrectAnswer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return detect(inQuestion, inAnswers, correctAnswer);
	}

	private static Question detect(final String question,
			final String[] answers, final String[] correct) {
		if (correct.length > SIMPLE_ANSWER_FLAG) {
			return new DiverseQuestion(question, answers, correct);
		}
		if (correct.length == SIMPLE_ANSWER_FLAG) {
			return new SimpleQuestion(question, answers, correct);
		} else {
			return new LogicalQuestion(question, answers, correct);
		}
	}

	/**
	 * Method to calculate arrays size from inputed string.
	 * 
	 * @param string - string to array
	 * @return size - size for new array
	 */
	static int sizeDetect(final String string) {
		Scanner sizeScan = new Scanner(string);
		int size = 0;
		while (sizeScan.hasNext()) {
			sizeScan.next();
			size++;
		}
		sizeScan.close();
		return size;
	}

	/**
	 * Method to manipulate questions using type detect algorithm.
	 */
	protected void questionsManipulate() {
		for (int i = 0; i < Manipulator.getTestSize(); i++) {
			Manipulator.systemQuestions[i] = Manipulator
											 .questionTypeDetect();
		}
	}

	public static int getTestSize() {
		return TEST_SIZE;
	}

	public static Question[] getSystemQuestions() {
		return systemQuestions;
	}

}
